package service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.Connection;

import entity.T_account;
import mapper.EmpMapper;
import util.BaseDao;

public class EmpService implements EmpMapper {

	@Override
	public List<T_account> queryAll() {
		Connection conn = null;
		String sql = null;
		try {
			sql = "select * from t_account";
			conn = (Connection) BaseDao.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet resultSet = ps.executeQuery();
			ArrayList<T_account> arrayList = new ArrayList<T_account>();
			while (resultSet.next()) {
				T_account t_account = new T_account();
				t_account.setId(resultSet.getInt("id"));
				t_account.setName(resultSet.getString("name"));
				t_account.setNumber(resultSet.getString("number"));
				t_account.setMoney(resultSet.getString("money"));
				arrayList.add(t_account);
			}
			if (arrayList.size() > 0) {
				return arrayList;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public int addAccount(T_account t_account) {
		Connection connection = null;
		try {
			String sql = "insert into t_account(name,number,money) values(?,?,?)";
			connection = (Connection) BaseDao.getConnection();
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, t_account.getName());
			ps.setString(2, t_account.getNumber());
			ps.setString(3, t_account.getMoney());
			int add = ps.executeUpdate();
			if (add > 0) {
				return add;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

	@Override
	public int updateAccount(T_account t_account) {
		Connection connection = null;
		try {
			String sql = "update t_account set name='" + t_account.getName() + "',number='" + t_account.getNumber()
					+ "',money='" + t_account.getMoney() + "' where id='"
					+ t_account.getId() + "'";
			System.out.println(sql);
			connection = (Connection) BaseDao.getConnection();
			PreparedStatement ps = connection.prepareStatement(sql);
			int update = ps.executeUpdate();
			if (update > 0) {
				return update;
			}
		} catch (Exception e) {
		}
		return 0;
	}

	@Override
	public int del(int id) {
		Connection connection = null;
		String sql = null;
		try {
			sql = "delete from t_account where id=" + id;
			connection = (Connection) BaseDao.getConnection();
			PreparedStatement ps = connection.prepareStatement(sql);
			int delUpdate = ps.executeUpdate();
			if (delUpdate > 0) {
				return delUpdate;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

}
